﻿@model WebApp.Models.Department
@{  ViewBag.Title = "部门信息 | Edit";} 
<!-- MAIN CONTENT -->
<div id="content">
     <!-- quick navigation bar -->
     <div class="row">
        <div class="col-xs-12 col-sm-7 col-md-7 col-lg-4">
            <h1 class="page-title txt-color-blueDark">
                <i class="fa fa-table fa-fw "></i>
                组织架构
                <span>
                    >
                    部门信息
                </span>
            </h1>
        </div>
        <div class="col-xs-12 col-sm-5 col-md-5 col-lg-8">

        </div>
     </div>
    <!-- widget grid -->
    <section id="widget-grid" class="">
        <!-- row -->
        <div class="row">
     <!-- NEW WIDGET START -->
    <article class="col-sm-12 col-md-12 col-lg-12">

        <!-- Widget ID (each widget will need unique ID)-->
        <div class="jarviswidget" id="wid-id-1" data-widget-editbutton="false" data-widget-deletebutton="false">
            <!-- widget options:
            usage: <div class="jarviswidget" id="wid-id-0" data-widget-editbutton="false">
            data-widget-colorbutton="false"
            data-widget-editbutton="false"
            data-widget-togglebutton="false"
            data-widget-deletebutton="false"
            data-widget-fullscreenbutton="false"
            data-widget-custombutton="false"
            data-widget-collapsed="true"
            data-widget-sortable="false"
            -->
            <header>
                <span class="widget-icon"> <i class="fa fa-edit"></i> </span>
                <h2> 部门信息 </h2>

            </header>

            <!-- widget div-->
            <div>

                <!-- widget edit box -->
                <div class="jarviswidget-editbox">
                    <!-- This area used as dropdown edit box -->

                </div>
                <!-- end widget edit box -->

                <!-- widget content -->
                <div class="widget-body">
                    <div class="widget-body-toolbar">
                        <div class="row">
                            <div class="col-sm-8  ">
                                <div class="btn-group">
                                    <a href="javascript:saveitem()" class="btn btn-sm btn-primary"> <i class="fa fa-floppy-o"></i> @Html.L("Save") </a>
                                </div>
                                <div class="btn-group" style="display:none">
                                    <a href="javascript:todo()" class="btn btn-sm btn-primary"> <i class="fa fa-print"></i> ToDo </a>
                                </div>
                            </div>
                            <div class="col-sm-4 text-align-right">
                                <div class="btn-group">
                                    <a href="/Departments/Index" class="btn btn-sm btn-success"> <i class="fa fa-chevron-left"></i> @Html.L("GoBack") </a>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="body-content">
                             @Html.Partial("EditForm")
                    </div>		 
                 </div>
                 <!-- end widget content -->
                 </div>
            <!-- end widget div -->

        </div>
        <!-- end widget -->


    </article>
    <!-- WIDGET END -->
  
 
</div>
 
</section>
</div>
<!-- END MAIN CONTENT -->

@section Scripts {
    <script type="text/javascript">
        var departmentid=@Model.Id;
	    var $editform = $('#department_form');
        //保存
        function saveitem() {
            if ($editform.valid()) {
                var department = $editform.serializeJSON();
                var token = $('input[name="__RequestVerificationToken"]', $editform).val();
                 $.ajax({
                    type: "POST",
                    url: "/Departments/EditAsync",
                    data: {
                        __RequestVerificationToken: token,
                        department:department
                    },
                    dataType: 'json',
                    contentType: 'application/x-www-form-urlencoded; charset=utf-8',
                })
                .done(response => {
                    if (response.success) {
                         toastr.success('保存成功'); 
                        window.location.href = "/Departments/Index";
                    } else {
                        $.messager.alert("错误", response.err, "error");
                    }
                    })
                .fail((jqXHR, textStatus, errorThrown) => {
                        $.messager.alert("异常",   errorThrown,"error");
                    });
            }
        }
        //ToDo
        function todo() {
        }

     /********************************
     *
     * pageSetUp() is needed whenever you load a page.
     * It initializes and checks for all basic elements of the page
     * and makes rendering easier.
     *
     */
     //pageSetUp();
     /********************************
     *
     * pagefunction() $editform 校验规则
     *
     */
     var pagefunction = function () {
            var errorClass = 'has-error';
            var successClass = 'has-success';
            var errorElement = 'em';
            $editform.validate({
                 errorElement: errorElement,
      errorPlacement: function (error, element) {
         // Add the `help-block` class to the error element
         error.addClass("help-block no-margin");
         if (element.prop("type") === "checkbox") {
          error.insertAfter(element.parent("label"));
         } else if (element.prop("type") === "date" || element.prop("type") === "datetime" ) {
          error.insertAfter(element.parent(".input-group"));
         } else {
          error.insertAfter(element);
         }
       },
       highlight: function (element, errorClass, validClass) {
        $(element).parents("div").addClass("has-error").removeClass("has-success");
       },
       unhighlight: function (element, errorClass, validClass) {
        $(element).parents("div").addClass("has-success").removeClass("has-error");
       },
                // Rules for form validation
                rules: {
                    Name:{
                            required:false,
                            rangelength: [1, 10],
                    },
                    Manager:{
                            required:false,
                            rangelength: [0, 10],
                    },
                    CompanyId:{
                            required:true,
                            digits: true,
                    },
                },
                // Messages for form validation
                messages:{
                
                },
                submitHandler:  (form) => {
                      //调用保存方法
                      saveitem();
                      return false;
                    //$(form).ajaxSubmit({
                    //    success: function () {
                    //        console.log('ajaxSubmit success');
                    //   }
                    //});
                },
                // Do not change code below
                errorPlacement: (error, element) => {
                    error.insertAfter(element.parent());
                }

            });
        }
        pagefunction();
  </script>
   <!-- 初始化from -->
 <script type="text/javascript">
    
    function initFormData(id) {
            //$('#department_form').form('load', data);
            
    }


   var departmentid=@Model.Id;
    $(function(){
        initFormData(departmentid);
    });
 </script>


}
 
